//
//  Progress.m
//  Progress
//
//  Created by lanou on 15/11/9.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "Progress.h"

@interface Progress ()
@property(nonatomic,strong)UIView *cacheView;
@end

@implementation Progress

{
    CGFloat centerX;//用于保存centreX的位置；
    
    
}


- (void)dealloc
{
    //代理置空
    _delegate = nil;
    
    //移除kvo；
    [self.thumb removeObserver:self forKeyPath:@"frame"];
    [self.slider removeObserver:self forKeyPath:@"value"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
        //我们继承于uiview而不是继承于UISlider的原因是 UISlider内部添加的任何view都处于progress线的下面 所以我们继承于uiview 把创建的uiview添加在整个slider的上面；
        
        //记得一定要是self.bounds self.frame的话可能会造成视图偏差；
        _slider = [[UISlider alloc]initWithFrame:self.bounds];
        
        //设置我们的slider的图片 给一个空的图片；
        [self.slider setThumbImage:[UIImage imageNamed:@"nil.png"] forState:UIControlStateNormal];
        
        [self.slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
         [self addSubview:self.slider];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [self addGestureRecognizer:tap];

         centerX = 0;
        
        
        //缓冲条在我们的slider的上面 滑块的下面；
        self.cacheView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 2)];
        //用户交互关闭
        self.cacheView.userInteractionEnabled = NO;
        [self addSubview:self.cacheView];
        
        
        //添加kvo 用户改变了frame的大小 我对center做一个修改
        [self.thumb addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
        
        //创建我们的滑块视图
        self.thumb = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
        self.thumb.center = CGPointMake(0, frame.size.height/2);
        self.thumb.userInteractionEnabled = NO;
        
        self.thumb.layer.cornerRadius = 5;
        self.thumb.layer.masksToBounds = YES;
        self.thumb.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.thumb];
        
        
        //添加一个kvo观察我们的slider的value值 然后对滑块的位置做修改；
        [self.slider addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    
    
    return self;
    
    
}

//kvo执行方法；
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    
    if ([keyPath isEqualToString:@"frame"])
    {
        self.thumb.center = CGPointMake(centerX, self.frame.size.height/2);
    }
    else
    {
        //修改滑块的位置；
        CGFloat progress = self.slider.value/(self.slider.maximumValue - self.slider.minimumValue);
        self.thumb.center = CGPointMake(self.frame.size.width * progress, self.frame.size.height/2);
        
    }
    
}


//滑动滑块执行的方法；
- (void)valueChange:(UISlider *)slider

{
    CGFloat progress = slider.value/(slider.maximumValue - slider.minimumValue);
    
    CGFloat thumbX = self.frame.size.width * progress;
    
    self.thumb.center = CGPointMake(thumbX, self.frame.size.height/2);
    
    //保存centerX的值
    centerX = self.thumb.center.x;
    
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    //我们要获取tap手势上的一个点；
    CGPoint point = [tap locationInView:tap.view];
    
    
    //判断范围
    if (point.x<0)
    {
        point.x = 0;
    }
    else if (point.x>self.frame.size.width)
    {
        point.x = self.frame.size.width;
        
    }
    
    
    //求出这个点所在的百分比；
    CGFloat progress = point.x/self.frame.size.width;
    
    //根据百分比计算出value的值
    CGFloat value = (self.slider.maximumValue - self.slider.minimumValue)*progress;
    
    self.slider.value = value;
    
    self.thumb.center = CGPointMake(point.x, self.frame.size.height/2);
    
    //传递参数；
    if ([_delegate respondsToSelector:@selector(touchView:)])
    {
        [_delegate touchView:value];
    }
    
}

//更换视图执行的方法；
- (void)setThumb:(UIView *)thumb
{
    //先移除观察者
    [_thumb removeObserver:self forKeyPath:@"frame"];
    
    //移除之前的视图；
    [_thumb removeFromSuperview];
    //添加新的视图
    [self addSubview:thumb];
    //指针赋值；
    _thumb = thumb;
    
    //添加新的观察者；
    [_thumb addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    
    //让它走一次kvo的方法，对center做改变；
    _thumb.frame = thumb.frame;
    
    
}

//外部更改frame 内部子视图也做出相应的更改；
- (void)setFrame:(CGRect)frame
{
    
    [super setFrame:frame];
    self.slider.frame = self.bounds;
    
    
}

//根据cacheValue进行赋值；
- (void)setCache:(CGFloat)cache
{
    //范围判断；
    if (cache<self.slider.minimumValue)
    {
        cache = self.slider.minimumValue;
    }
    else if (cache>self.slider.maximumValue)
    {
        cache = self.slider.maximumValue;
        
    }
    
    CGFloat progress = cache/(self.slider.maximumValue - self.slider.minimumValue);
    
    //对cacheView的frame进行赋值
    self.cacheView.frame = CGRectMake(0, (self.frame.size.height-2)/2, progress*self.frame.size.width, 2);
    
    
}

//更改我们缓冲条的背景颜色；
- (void)setCacheColor:(UIColor *)cacheColor
{
    self.cacheView.backgroundColor = cacheColor;
    
    
}

@end
